package com.mall.widget;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.amap.api.location.AMapLocation;
import com.examination.mlib.bean.DefaultEntity;
import com.examination.mlib.constants.AllStringConstants;
import com.examination.mlib.constants.ClickApi;
import com.examination.mlib.constants.KitArouterConstants;
import com.examination.mlib.utils.LocationUtils;
import com.mall.MallUtils;
import com.mall.R;
import com.mall.bean.YCategoryListBean;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.GlideUtil;
import com.yilijk.base.utils.ListUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MallClassifyView extends LinearLayout {
    private Context context;
    private View contentView;
    private RecyclerView recyclerView;
    private List<YCategoryListBean.ListBean> listBeans;
    private ClassifyAdapter adapter;
    private String doctorId;

    public MallClassifyView(Context context) {
        super(context);
        init(context);
    }

    public MallClassifyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MallClassifyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public MallClassifyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        contentView = LayoutInflater.from(context).inflate(R.layout.view_mall_classify, this);

        recyclerView = contentView.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(context, 5));

    }

    public void setCategoryListBean(YCategoryListBean categoryListBean, String doctorId) {
        this.doctorId = doctorId;
        if (categoryListBean != null && ListUtil.getSize(categoryListBean.getList()) > 0) {
            listBeans = categoryListBean.getList();
            adapter = new ClassifyAdapter(context);
            recyclerView.setAdapter(adapter);
        }
    }

    public class ClassifyAdapter extends RecyclerView.Adapter {
        private Context context;

        public ClassifyAdapter(Context context) {
            this.context = context;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_mall_classify, parent, false);
            return new ClassifyVH(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof ClassifyVH) {
                ((ClassifyVH) holder).setData(position);
            }
        }

        @Override
        public int getItemCount() {
            return ListUtil.getSize(listBeans);
        }
    }

    public class ClassifyVH extends RecyclerView.ViewHolder {

        private ImageView iconIv;
        private TextView titleTv;

        public ClassifyVH(@NonNull View itemView) {
            super(itemView);
            iconIv = itemView.findViewById(R.id.iv_icon);
            titleTv = itemView.findViewById(R.id.tv_title);
        }

        public void setData(int position) {
            YCategoryListBean.ListBean bean = listBeans.get(position);
            GlideUtil.load(context, bean.getImg().getImgUrl(), iconIv, R.drawable.icon_default_square);
            titleTv.setText(bean.getSubTitle());

            ClickUtils.setFastOnClickListener(itemView, new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ClickUtils.sendEvent(ClickApi.Mall.click_drugsclassify, context, "电商首页药品分类点击");
                    String appRoute = bean.getImg().getAppRoute();
                    String[] loadArray = appRoute.split("load=");
                    if (loadArray.length >= 2) {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", bean.getSubTitle());
                        if (!TextUtils.isEmpty(loadArray[1])) {
//                            bundle.putString("frontCategoryId", loadArray[1]);
//                            ArrayList<String> ids = new ArrayList<>();
//                            ids.add(loadArray[1]);
                            String[] loads = loadArray[1].split(",");
//                            List<String> ids = Arrays.asList(loads);
                            bundle.putStringArray("frontCategoryIds", loads);
                        }
                        if (!TextUtils.isEmpty(doctorId)) {
                            bundle.putString("doctorId", doctorId);
                        }
                        DefaultEntity.DataBean whCityBean =  LocationUtils.getInstance().getWhCityBean();
                        AMapLocation aMapLocation =  LocationUtils.getInstance().getaMapLocation();
                        if (whCityBean != null && !TextUtils.isEmpty(whCityBean.getProvinceName()) && !TextUtils.isEmpty(whCityBean.getCityName())) {
                            bundle.putString("whCity", whCityBean.getProvinceName() + "," + whCityBean.getCityName());
//                            paramsJSON.put("lat", whCityBean.getLat());
//                            paramsJSON.put("lng", whCityBean.getLng());
                        } else if (aMapLocation != null && !TextUtils.isEmpty(aMapLocation.getProvince()) && !TextUtils.isEmpty(aMapLocation.getCity())) {
                            bundle.putString("whCity", aMapLocation.getProvince() + "," + aMapLocation.getCity());
//                            paramsJSON.put("lat", aMapLocation.getLatitude());
//                            paramsJSON.put("lng", aMapLocation.getLongitude());
                        }
                        MallUtils.push(context, KitArouterConstants.CART_DOCTORBUNDLE, KitArouterConstants.Cart.Retailers, bundle);
                    }
                }
            });

        }


    }
}
